﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;

namespace BLL
{
    public static class zy_BLL
    {
        private static dbExamDataContext db = new dbExamDataContext();
        /// <summary>
        /// 获取专业数据源
        /// </summary>
        public static object GetSource()
        {
            object t = from p in db.ZY orderby p.yxNum select new { p.zyNum,zyZF=(p.zyZF==true?"是":"否"), p.zyName, p.yxNum, p.YX.yxName, p.zxkm };
            return t;
        }

        /// <summary>
        /// 获取专业数据源
        /// </summary>
        /// <param name="yxNum">院系编号</param>
        public static object GetSource(string yxNum)
        {
            object obj = new object();
            obj = from p in db.ZY orderby p.yxNum where p.yxNum==decimal.Parse(yxNum) select new { p.zyNum, zyZF = (p.zyZF == true ? "是" : "否"), p.zyName, p.yxNum, p.YX.yxName, p.zxkm };
            return obj;
        }

        /// <summary>
        /// 添加新的专业数据
        /// </summary>
        /// <param name="zy">新的专业数据对象</param>
        public static bool Add(ZY zy)
        {
            bool bl = false;
            try
            {
                //验证数据是否存在
                if (db.ZY.Single(p => p.zyName == zy.zyName).zyNum > -1)
                    return false;
            }
            catch { }
            //准备数据
            db.ZY.InsertOnSubmit(zy);
            //执行更新
            db.SubmitChanges();
            bl = true;
            return bl;
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="zyNum">要更新的数据标识</param>
        /// <param name="newZY">新的数据对象</param>
        public static bool Update(string zyNum, ZY newZY)
        {
            bool bl = false;
            try
            {
                ZY zy = db.ZY.Single(p => p.zyNum == decimal.Parse(zyNum));
                zy.yxNum = newZY.yxNum;
                zy.zyName = newZY.zyName;
                zy.zxkm = newZY.zxkm;
                zy.zyZF = newZY.zyZF;
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="zyNum">要删除的数据的标识</param>
        public static bool Delete(string zyNum)
        {
            db = new dbExamDataContext();
            bool bl = false;
            try {
                ZY zy = db.ZY.Single(p => p.zyNum ==decimal.Parse( zyNum));
                db.ZY.DeleteOnSubmit(zy);
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }
    }
}
